package com.bzyd.mp.datascope.strategy;

import com.bzyd.mp.datascope.DataScopeParam;
import com.bzyd.mp.utils.UserContextUtil;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
import net.sf.jsqlparser.schema.Column;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;

/**
 * 自定义策略，仅部门老大角色可以查看自己部门及子部门的 员工只能查询自己的
 */
public class CustomDataScopeStrategy implements DataScopeStrategy {

    @Override
    public String mode() {
        return "custom";
    }

    @Override
    public Expression buildWhere(Expression where, String mappedStatementId, DataScopeParam dataScopeParam) {

        Set<String> roleCodes = UserContextUtil.getRoleCodes();

        //公司总经理、部门经理角色
        if (roleCodes.contains("R001") || roleCodes.contains("R002")) {
            return grantOwnChildDept(where, dataScopeParam, UserContextUtil.getDeptCode());
        } else {
            return grantOwn(where, dataScopeParam, UserContextUtil.getCurrentUser());
        }
    }
}
